%{
#define YYERROR_VERBOSE
#include <stdio.h>
#include <string.h>
#include "syncconf.h"

extern FILE *syncconfin;

void yyerror(struct syncconf* conf, char *err)
{
    printf("%s\n", err);
}

int syncconf(struct syncconf* conf)
{
    FILE *cfgfile = fopen("./sync.conf", "r");
    if (cfgfile) {
        syncconfin = cfgfile;
        yyparse(conf);
        fclose(cfgfile);
    } else {
        printf("error open file %s\n", "./sync.conf");
    }
}

%}

%parse-param {struct syncconf* conf}

%union
{
    char*            str;
    int              num;
    struct syncconf* conf;
}

%token EQUAL SCHEMANAME NODENAME ODBC_SOURCE ARCHIVE_INBOX ARCHIVE_OUTBOX ARCHIVE_STRUCTURE LOG_LEVEL
%token <str> WORD STRUCTURE
%token <num> YESNO LEVEL
%type <str> SCHEMANAME_DEF NODENAME_DEF ODBC_SOURCE_DEF ARCHIVE_STRUCTURE_DEF
%type <num> ARCHIVE_INBOX_DEF ARCHIVE_OUTBOX_DEF LOG_LEVEL_DEF
%type <conf> CONFIG

%start CONFIG

%%
CONFIG: SCHEMANAME_DEF NODENAME_DEF ODBC_SOURCE_DEF ARCHIVE_INBOX_DEF ARCHIVE_OUTBOX_DEF ARCHIVE_STRUCTURE_DEF LOG_LEVEL_DEF {
    $$ = conf;
    $$->schemaname=$1;
    $$->nodename=$2;
    $$->source=$3;
    $$->archiveinbox=$4;
    $$->archiveoutbox=$5;
    $$->archivestructure=$6;
    $$->loglevel=$7;
};
SCHEMANAME_DEF: SCHEMANAME EQUAL WORD {
    $$=$3;
};
NODENAME_DEF: NODENAME EQUAL WORD {
    $$=$3;
};
ODBC_SOURCE_DEF: ODBC_SOURCE EQUAL WORD {
    $$=$3;
};
ARCHIVE_INBOX_DEF: ARCHIVE_INBOX EQUAL YESNO {
    $$=$3;
};
ARCHIVE_OUTBOX_DEF: ARCHIVE_OUTBOX EQUAL YESNO {
    $$=$3;
};
ARCHIVE_STRUCTURE_DEF: ARCHIVE_STRUCTURE EQUAL STRUCTURE {
    $$=$3;
};
LOG_LEVEL_DEF: LOG_LEVEL EQUAL LEVEL {
    $$=$3;
};
%%
